package com.study.talk.app.friend;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;

import com.bumptech.glide.Glide;
import com.study.talk.R;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.app.moment.MomentListActivity;
import com.study.talk.databinding.ActivityUserInfoBinding;
import com.study.talk.entity.UserInfo;
import com.study.talk.entity.resp.BoolResponse;
import com.study.talk.entity.vo.FriendVo;
import com.study.talk.net.NetApi;
import com.study.talk.netty.NettyClient;
import com.study.talk.netty.message.request.AddUserRequest;
import com.study.talk.netty.message.response.AddUserSelfResponse;
import com.study.talk.util.DateUtil;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import io.netty.util.internal.StringUtil;

public class UserInfoActivity extends BaseActivity {


    private UserInfo userInfo;

    private ActivityUserInfoBinding activityBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityBinding = ActivityUserInfoBinding.inflate(getLayoutInflater());
        setContentView(activityBinding.getRoot());
        setSupportActionBar(activityBinding.userInfoToolbar);

        Integer userId = getIntent().getIntExtra("userId", -1);

        NetApi.getUserInfo(userId, data -> {
            userInfo = data;
            runOnUiThread(this::initView);
        }, msg -> runOnUiThread(() -> ToastUtil.showToast(UserInfoActivity.this, msg)));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.moment, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @SuppressLint("SetTextI18n")
    private void initView() {

        activityBinding.userInfoToolbar.setNavigationOnClickListener(v -> finish());
        activityBinding.userInfoToolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.user_info_moment) {
                Intent intent = new Intent(this, MomentListActivity.class);
                intent.putExtra(MomentListActivity.typeName, MomentListActivity.MOMENT_FRIEND);
                intent.putExtra(MomentListActivity.dataName, userInfo.getId());
                startActivity(intent);
            }
            return true;
        });


        Glide.with(UserInfoActivity.this).load(OkHttp.URL + userInfo.getAvatar()).into(activityBinding.userInfoHeadImg);

        if (!StringUtil.isNullOrEmpty(userInfo.getBgImg())) {
            Glide.with(UserInfoActivity.this).load(OkHttp.URL + userInfo.getBgImg()).into(activityBinding.userInfoBgImg);
        }

        activityBinding.userInfoNickname.setText(userInfo.getNickName());
        activityBinding.userInfoUsername.setText("账号：" + userInfo.getUsername());
        activityBinding.userInfoListView.setAdapter(new ArrayAdapter<String>(UserInfoActivity.this, android.R.layout.simple_list_item_1, createListViewData()));

        activityBinding.userInfoAddBt.setOnClickListener(view -> {
            AddUserRequest request = new AddUserRequest(getApp().userId(), userInfo.getId());
            request.setDateTime(DateUtil.getCurrentTime());
            NettyClient.sendMessage(request);
            showLoadingDialog();
        });


        OkHttp.post("/friend/isFriend", GsonInstance.instance.toJson(new FriendVo(getApp().userId(), userInfo.getId())), res -> {
            BoolResponse response = GsonInstance.instance.fromJson(res, BoolResponse.class);
            if (response.getData()) {
                runOnUiThread(() -> {
                    activityBinding.userInfoAddBt.setVisibility(View.GONE);
                });
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAddUserSelfResponseMessage(AddUserSelfResponse addUserSelfResponse) {
        dismissLoadingDialog();
        if (addUserSelfResponse.getSuccess()) {
            ToastUtil.showToast(UserInfoActivity.this, "好友请求发送成功!");
        } else {
            ToastUtil.showToast(UserInfoActivity.this, "发送失败:" + addUserSelfResponse.getReason());
        }
    }


    private List<String> createListViewData() {
        List<String> listViewData = new ArrayList<>();
        listViewData.add("性别：" + (userInfo.getGender() == 1 ? "男" : "女"));
        listViewData.add("生日：" + userInfo.getBirth());
        listViewData.add("电话：" + userInfo.getPhone());
        listViewData.add("邮箱：" + userInfo.getEmail());
        listViewData.add("地址：" + userInfo.getAddress());
        listViewData.add("简介：" + userInfo.getIntroduce());
        return listViewData;
    }
}